package com.xc.controller;

import com.xc.dto.ResultDTO;
import com.xc.entity.UserRoleEntity;
import com.xc.entity.UserRoleManyEntity;
import com.xc.service.UserRoleService;
import com.xc.util.Tool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//restful 增加post，修改put，删除delete，查询get
@RestController
@RequestMapping("/xc/userRole")
@Api(tags = "用户角色管理")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserRoleEntity userRoleEntity;

    //    @PostMapping()
//    @ApiOperation("用户角色增加,分开传数据")
//    public ResultDTO add(@RequestBody UserRoleEntity userRoleEntity) {
//
//        return userRoleService.add(userRoleEntity);
//    }
    @PostMapping()
    @ApiOperation("用户角色增加,分开传数据")
    public ResultDTO add(@RequestBody List<UserRoleManyEntity> list) {

        return userRoleService.addMany(list);
    }
//    @PutMapping()
//    @ApiOperation("用户角色修改")
//    public ResultDTO update(@RequestBody
//                                    UserRoleEntity userRoleEntity) {
//        return userRoleService.update(userRoleEntity);
//    }

    @ApiOperation("用户角色修改状态enable，1表示正常，0表示未达到标准")
    @PutMapping("/enable")
    public ResultDTO updateEnable(@RequestBody UserRoleEntity userRoleEntity) {
        return userRoleService.updateEnable(userRoleEntity);
    }

//    @DeleteMapping("/{id}")
//    @ApiOperation("用户角色删除")
//    public ResultDTO delete(UserRoleEntity userRoleEntity) {
//        return userRoleService.delete(userRoleEntity);
//    }

    //    @GetMapping()
//    @ApiOperation("用户角色查询,直接第二个参数传递json对象即可")
//    public ResultDTO findAll(@ApiParam(value = "用户角色名") @RequestParam String username,
//                             @ApiParam(value = "性别") @RequestParam String sex,
//                             @ApiParam(value = "年龄") @RequestParam Integer age,
//                             @ApiParam(value = "手机") @RequestParam String phone,
//                             @ApiParam(value = "生日") @RequestParam String birthday,
//                             @ApiParam(value = "邮箱") @RequestParam String email,
//                             @ApiParam(value = "第几页") @RequestParam String page,
//                             @ApiParam(value = "每页显示条数") @RequestParam String limit) {
////        userEntity = JSONCopy.copy(userVO, UserEntity.class);
//        return userService.findAll(UserEntity.builder()
//                .username(username)
//                .sex(sex)
//                .age(age)
//                .phone(phone)
//                .birthday(birthday)
//                .email(email)
//                .page(Tool.strToPage(page))
//                .limit(Tool.strToPage(limit))
//                .build());
//    }
    @GetMapping()
    @ApiOperation("用户角色查询,直接第二个参数传递json对象即可")
    public ResultDTO findAll(UserRoleEntity userRoleEntity) {
        userRoleEntity.setPage(Tool.strToPage(userRoleEntity.getPage() + ""));
        userRoleEntity.setLimit(Tool.strToLimit(userRoleEntity.getLimit() + ""));
        return userRoleService.findAll(userRoleEntity);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户角色")
    public ResultDTO findById(@PathVariable String id) {
        userRoleEntity.setId(Tool.strToInt(id));
        return userRoleService.findById(userRoleEntity);
    }

    @GetMapping("/findByUserId")
    @ApiOperation("根据用户id查询用户角色")
    public ResultDTO findByUserId(UserRoleEntity userRoleEntity) {
//        userRoleEntity.setUserId(Tool.strToInt(userId));
        return userRoleService.findByUserId(userRoleEntity);
    }

    @GetMapping("/count")
    @ApiOperation("查询总条数")
    public ResultDTO count(UserRoleEntity userRoleEntity) {
        return userRoleService.count(userRoleEntity);
    }
}
